// pages/home/home.js
var api = require('../../utils/api.js');
import { String } from '../../utils/util.js';
// wx.downloadFile({
//   url: 'https://cum.img.livestaring.com/2.pdf',
//   success: function (res) {
//     var filePath = res.tempFilePath
//     wx.openDocument({
//       filePath: filePath,
//       success: function (res) {
//         console.log('打开文档成功')
//       }
//     })
//   },
//   fail:function(res){
//     console.log(res);
//   },
//   complete: function (res) {
//     console.log(res);
//   }
// });

Page({
  /**
   * 页面的初始数据
   */
  data: {
    controls: false,
    show_play_btn: false,
    page_current: 1,
    page_count: '',
    page_current_demand: '1',
    page_count_demand: '',
    userbaseinfo: api.getStorageData().userbaseinfo,
    isIpx: api.isIpx ? true : false,
    categories: [
      { id: 0, name: "首页" },
      { id: 1, name: "直播" },
      { id: 2, name: "视频" },
      { id: 3, name: "需求" }
    ],

    sort: [

    ],
    activeCategoryId: 0, //tabid
    selectCurrent: 0,
    swiperconfig: {
      banners: [
      ],
      circular: false,
      indicatorDots: true,
      autoplay: false,
      interval: 2000,
      duration: 800,
      loadingHidden: false, // loading
      userInfo: {},
      swiperCurrent: 0,
      categories: [],
      goods: [],
      scrollTop: "0",
      loadingMoreHidden: true,

      hasNoCoupons: true,
      coupons: [],
      searchInput: '',
    }

  },

  //我的
  launch: function () {
    var token = api.getToken();
    if (String.isBlank(token)) {
      wx.navigateTo({
        url: '../login/login',
      })
      return;
    }
    wx.reLaunch({
      url: '../user/userinfo/userinfo'
    })
  },
  palyVideo: function (res) {
    console.log(res);
  },

  //视频列表
  getVideoData: function () {
    let that = this;
    api.post({
      url: 'Home/video',
      data: {
        token: that.data.userbaseinfo.token,
        page: 1
      },
      success: function (data) {
        console.log(data);
        if (data.code == 200) {
          data = data.data;
          that.setData({
            arraylist_video: data.lists
          });
        }
      }
    })
  },

  //需求列表
  getDemandData: function () {
    let that = this;
    api.post({
      url: 'Home/demand',
      data: {
        token: that.data.userbaseinfo.token,
        page: that.data.page_current_demand
      },
      success: function (data) {
        console.log(data);
        if (data.code == 200) {
          data = data.data;
          that.setData({
            array_demand: data.lists
          });


        }

      }
    })



  },

  // 首页分类切换
  tabClick: function (e) {
    var that = this;
    this.setData({
      activeCategoryId: e.currentTarget.id
    });

    switch (parseInt(e.currentTarget.id)) {
      case 0:

        break;
      case 1:

        break;
      case 2:
        that.getVideoData();
        break;
      case 3:
        that.getDemandData();
        break;
      default:
    }


  },
  //事件处理函数
  swiperchange: function (e) {
    //console.log(e.detail.current)
    this.setData({
      'swiperconfig.swiperCurrent': e.detail.current
    });

  },
  toDetailsTap: function (e) {
    wx.navigateTo({
      // url: "/pages/goods-details/index?id=" + e.currentTarget.dataset.id
    })
  },
  //banner
  tapBanner: function (e) {
    var token = api.getToken();
    if (String.isBlank(token)) {
      wx.navigateTo({
        url: '../login/login',
      })
      return;
    }
    if (e.currentTarget.dataset.id != 0) {
      wx.navigateTo({
        // url: "/pages/goods-details/index?id=" + e.currentTarget.dataset.id
      })
    }
  },
  bindTypeTap: function (e) {
    console.log(e);
    this.setData({
      'swiperconfig.selectCurrent': e.index
    })
  },

  //tabBar 上传 开播 发布
  startLive: function () {
    var token = api.getToken();
    if (String.isBlank(token)) {
      wx.navigateTo({
        url: '../login/login',
      })
      return;
    }
    wx.showActionSheet({
      itemList: ['开启直播', '上传视频', '发布需求'],
      success: function (res) {
        console.log(res);
        res.tapIndex
        switch (res.tapIndex) {
          case 0:
            wx.navigateTo({
              url: '../liveseting/liveseting',
            });
            break;
          case 1:
            wx.navigateTo({
              url: '../uploadvideo/uploadvideo',
            });
            break;
          case 2:
            wx.navigateTo({
              url: '../senddemand/senddemand',
            });
            break;
          default:

        }
      },
      fail: function (res) {
        console.log(res.errMsg);
      }
    });
  },

  navToLogin: function () {
    var token = api.getToken();
    if (String.isBlank(token)) {
      wx.navigateTo({
        url: '../login/login',
      })
      return;
    }
  },
  navToSearch: function () {
    var token = api.getToken();
    if (String.isBlank(token)) {
      wx.navigateTo({
        url: '../login/login',
      })
      return;
    }
    wx.navigateTo({
      url: '../homesearch/homesearch',
    })
  },


  homeDataRequest: function () {
    let that = this;
    api.post({
      url: 'Home/index',
      data: {
        token: that.data.userbaseinfo.token,
        page: that.data.page_current
      },
      success: function (data) {
        console.log(data);
        if (data.code == 200) {
          // let lists = data.data.lists;
          data = data.data;
          that.setData({
            sort: data.group.lists,
            array_live: data.live.lists,
            array_video: data.video.lists,
            'swiperconfig.banners': data.slide_1.lists,
            ad_src: data.slide_2.lists[0]
          });


        }

      }
    })
  },
  navToNext: function (e) {
    let that = this;
    console.log(e.currentTarget.dataset.navurl);
    if (!e.currentTarget.dataset.price == 0) {
      let price = e.currentTarget.dataset.price;
      wx.showModal({
        title: '付费观看',
        content: '价格:' + price + '元',
        success: function (res) {
          if (res.confirm) {
            var token = that.data.userbaseinfo.token;
            if (String.isBlank(token)) {
              wx.navigateTo({
                url: '../login/login',
              })
              return;
            }
            let url = e.currentTarget.dataset.navurl;
            wx.navigateTo({
              url: url
            });
          } else if (res.cancel) {

          }
        }
      })

    } else {
      var token = that.data.userbaseinfo.token;
      if (String.isBlank(token)) {
        wx.navigateTo({
          url: '../login/login',
        })
        return;
      }
      let url = e.currentTarget.dataset.navurl;
      wx.navigateTo({
        url: url
      });
    }



  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      'swiperconfig.banners[0].picUrl':
      'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
      userbaseinfo: api.getStorageData().userbaseinfo,
    });
    var that = this;
    that.homeDataRequest();

    // that.homeDataRequest();

    // function req(){
    //   if (that.data.token&&that.data.token!='') {
    //     that.homeDataRequest();
    //   }else{
    //     req();
    //   }
    // }

    // req();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh()


  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})